package cn.edu.glut.choosethesis.service.impl;


import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import cn.edu.glut.choosethesis.repository.SubjectRepository;
import cn.edu.glut.choosethesis.service.SubjectService;
import cn.edu.glut.entity.Subject;
import cn.edu.glut.vo.SubjectToTeacherVO;

@Service
public class SubjectServiceImpl implements SubjectService{

	@Autowired
	SubjectRepository subjectRepository;
	
	@Override
	public int save(Subject s) {
		//供选数量与剩余数量初始化一致
		s.setNumber(s.getOffer());
		subjectRepository.save(s);
		if(!StringUtils.isEmpty(s.getId()))
			return 1;
		return 0;
	}

	@Override
	public void delete(String id) {
		subjectRepository.delete(id);
	}

	@Override

	@Transactional
	public int update(Subject news) {
		Subject olds = subjectRepository.findOne(news.getId());
		Preconditions.checkNotNull(olds, "该题目不存在");
		olds.updateData(news);
		subjectRepository.save(olds);
		return 1;
	}

	@Override
	public Subject findById(String id) {
		return subjectRepository.findOne(id);
	}

	@Override
	public Page<SubjectToTeacherVO> list(Pageable pageable, Boolean isSelect, String titleLike, String teacherId, String majorId, String studentId) {
		return subjectRepository.findAllSubjectToTeacherVO(pageable, isSelect, titleLike, teacherId, majorId, studentId);
	}

	@Override
	public void deleteAll() {
		subjectRepository.deleteAllInBatch();
	}

}
